# Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2015 ECMWF. This software is distributed under the terms
#  of the Apache License version 2.0. In applying this license, ECMWF does not
#  waive the privileges and immunities granted to it by virtue of its status as
#  an Intergovernmental Organization or submit itself to any jurisdiction.
# 
#  ***************************** LICENSE END ************************************

lsm = read('land_sea_mask.grib')
lsm = (lsm >= 0.5)
#plot(lsm)

t2m = read('t2m.grib')
all_mean = integrate(t2m)
print("means of original fields: ", all_mean)

lsm = bitmap(lsm, 0) # turn zeros into missing values
# plot(lsm)

t2m = bitmap(t2m, lsm) # copy the missing value mask to the t2m field
#plot(t2m)

land_mean = integrate(t2m) # compute the mean values from each field
if land_mean = nil then
    print('No valid data points')
else
    print("means of just land points: ", land_mean)
end if

print("average: ", average(t2m))
print("accumulate lsm: ", accumulate(lsm))
print("datainfo: ", datainfo(t2m))
